/**
 * Filename: LocalManageAdapter.java
 *
 * @author Firefly
 * @contact postfirefly@163.com
 * @time 2016/3/17 15:22
 */
package com.github.postsanf.yinian.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.ScaleAnimation;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.facebook.drawee.view.SimpleDraweeView;
import com.github.postsanf.yinian.R;
import com.github.postsanf.yinian.activity.ImageLocalSelectActivity;
import com.github.postsanf.yinian.activity.ImageLocalZoomBigActivity;
import com.github.postsanf.yinian.constants.SelectedImg;
import com.github.postsanf.yinian.utils.LocalViewHolder;
import com.github.postsanf.yinian.utils.StringUtils;
import com.github.postsanf.yinian.utils.UIHelper;

import java.util.List;

public class LocalManageImageAdapter extends CommonAdapter2<String> {
    private Context mContext;

    public LocalManageImageAdapter(Context context, List<String> mDatas, int itemLayoutId) {
        super(context, mDatas, itemLayoutId, 0);
        this.mContext = context;
    }

    @Override
    public void convert(final LocalViewHolder helper, final String item, final int position, final Boolean isFirst) {
        final SimpleDraweeView mImageView = helper.getView(R.id.iv_album_grid_item_image);
        final ImageView mSelect = helper.getView(R.id.id_item_select);
        final RelativeLayout mSelectbg = helper.getView(R.id.id_item_select_bg);
        if (!StringUtils.isEmpty(item)) {
            helper.setImageByUrl(R.id.iv_album_grid_item_image, item);
        }
        int itemWidth = UIHelper.getAlbumItemHighWidth(mContext);
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(itemWidth, itemWidth);
        mImageView.setLayoutParams(params);
        mSelectbg.setLayoutParams(params);
        mSelect.setOnClickListener(new View.OnClickListener() {
            //选择，则将图片变暗，反之则反之
            @Override
            public void onClick(View v) {
                if (SelectedImg.mSelectedImage.size() < UIHelper.MAX_IMG_COUNT) {
                    // 已经选择过该图片
                    if (SelectedImg.mSelectedImage.contains(item)) {
                        SelectedImg.mSelectedImage.remove(item);
                        mSelect.setImageResource(R.drawable.image_selected_before);
                        mSelectbg.setVisibility(View.GONE);
                        ScaleAnimation scaleAnimation = new ScaleAnimation(0.8f, 1.1f, 0.8f, 1.1f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
                        mSelect.setAnimation(scaleAnimation);
                        scaleAnimation.startNow();
                    } else {
                        // 未选择该图片
                        SelectedImg.mSelectedImage.add(item);
                        mSelect.setImageResource(R.drawable.image_selected_after);
                        mSelectbg.setVisibility(View.VISIBLE);
                        ScaleAnimation scaleAnimation = new ScaleAnimation(1.1f, 0.8f, 1.1f, 0.8f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
                        mSelect.setAnimation(scaleAnimation);
                        scaleAnimation.startNow();
                    }
                    mContext.sendBroadcast(new Intent("updatetopgridview"));
                } else if (SelectedImg.mSelectedImage.size() >= UIHelper.MAX_IMG_COUNT) {
                    if (SelectedImg.mSelectedImage.contains(item)) {
                        SelectedImg.mSelectedImage.remove(item);
                        mSelect.setImageResource(R.drawable.image_selected_before);
                        mSelectbg.setVisibility(View.GONE);
                    } else {
                        //超过限制张数
                    }
                }
            }
        });
        mImageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (ImageLocalSelectActivity.mImgs.size() == 0)
                    return;
                Intent zoomintent = new Intent(mContext, ImageLocalZoomBigActivity.class);
                zoomintent.putExtra("typelocalAll", true);
                zoomintent.putExtra("position", position);
                mContext.startActivity(zoomintent);
            }
        });
        // 已经选择过的图片，显示出选择过的效果
        if (SelectedImg.mSelectedImage.contains(item)) {
            mSelect.setImageResource(R.drawable.image_selected_after);
            mSelectbg.setVisibility(View.VISIBLE);
//            ScaleAnimation scaleAnimation = new ScaleAnimation(1.0f, 0.9f, 1.0f, 0.9f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
//            scaleAnimation.setFillAfter(true);
//            mImageView.setAnimation(scaleAnimation);
//            scaleAnimation.startNow();
        } else {
            mSelect.setImageResource(R.drawable.image_selected_before);
            mSelectbg.setVisibility(View.GONE);
//            ScaleAnimation scaleAnimation = new ScaleAnimation(0.9f, 1.0f, 0.9f, 1.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
//            scaleAnimation.setFillAfter(true);
//            mImageView.setAnimation(scaleAnimation);
//            scaleAnimation.startNow();
        }
    }

}